package com.yungu.swift.assets.passenger.service.impl;

import com.github.miemiedev.mybatis.paginator.domain.Order;
import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import com.github.miemiedev.mybatis.paginator.domain.PageList;
import com.yungu.swift.assets.passenger.dao.PassengerEmergencyMemberMapper;
import com.yungu.swift.assets.passenger.model.dto.PassengerEmergencyMemberDto;
import com.yungu.swift.assets.passenger.model.param.AdminPassengerUuidPageParam;
import com.yungu.swift.assets.passenger.service.PassengerEmergencyMemberService;
import com.yungu.swift.base.ResponseData;
import com.yungu.swift.base.mapper.IMapper;
import com.yungu.swift.base.model.PageVo;
import com.yungu.swift.base.service.impl.BaseServiceImpl;
import com.yungu.swift.utils.MapUtils;
import com.alibaba.dubbo.config.annotation.Service;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * <p>
 * 乘客紧急人成员表
 *
 * @author Zerrion
 * @version 1.0.3-RELEASE
 * @since 2018-10-16 10:35:39
 */

@Service
public class PassengerEmergencyMemberServiceImpl extends BaseServiceImpl<PassengerEmergencyMemberDto> implements PassengerEmergencyMemberService {

    @Autowired
    private PassengerEmergencyMemberMapper passengerEmergencyMemberMapper;

    @Override
    protected IMapper<PassengerEmergencyMemberDto> getMapper() {
        return passengerEmergencyMemberMapper;
    }

    @Override
    public ResponseData<Boolean> patchUpdateStatus(String emergencyUuid, Integer emergencyStatus) {
        int patchCode = passengerEmergencyMemberMapper.patchUpdateStatus(MapUtils.build(
                new String[]{"emergencyUuid", "emergencyStatus", "updateOn"}, new Object[]{emergencyUuid, emergencyStatus, new Date()}));
        if (patchCode > 0) {
            return ResponseData.buildSuccessResponse("批量更细成功", true);
        }
        return ResponseData.buildErrorResponse(ResponseData.ERROR_CODE, "批量更细失败");
    }

    @Override
    public ResponseData<PageVo<PassengerEmergencyMemberDto>> queryPage(AdminPassengerUuidPageParam param) {
        PageBounds pageBounds = new PageBounds(param.getPageNum(), param.getPageSize());
        if (StringUtils.isNotEmpty(param.getSortString())) {
            pageBounds.setOrders(Order.formString(param.getSortString()));
        }
        PageList<PassengerEmergencyMemberDto> pageList = passengerEmergencyMemberMapper.queryPage(MapUtils.build("passengerUuid",param.getPassengerUuid()), pageBounds);
        PageVo<PassengerEmergencyMemberDto> pageVo = new PageVo(param.getPageNum(), param.getPageSize(), pageList.getPaginator().getTotalCount());
        pageVo.setItems(pageList);
        return ResponseData.buildSuccessResponse(pageVo);
    }

    @Override
    public ResponseData<Integer> updateIsDefault(AdminPassengerUuidPageParam passengerUuidPageParam) {
        List<PassengerEmergencyMemberDto> list = passengerEmergencyMemberMapper.list(
                MapUtils.build(new String[]{"passengerUuid", "isDefault"},
                        new Object[]{passengerUuidPageParam.getPassengerUuid(), 1}));
        //修改已经是默认的为非默认状态
        if(!list.isEmpty()) {
            for (PassengerEmergencyMemberDto dto : list) {
                Map<String, Object> param = new HashMap<>();
                param.put("uuid",dto.getUuid());
                param.put("isDefault",0);
                passengerEmergencyMemberMapper.updateIsDefault(param);
            }
        }
        Map<String, Object> param = new HashMap<>();
        param.put("uuid",passengerUuidPageParam.getUuid());
        param.put("isDefault",1);
        return ResponseData.buildSuccessResponse(passengerEmergencyMemberMapper.updateIsDefault(param));
    }

}
